#include "StdAfx.h"
#include "MaterialEditorStartup.h"
#include "materialeditor.h"

namespace Lit
{
	namespace Editor
	{
		namespace Material
		{
			StartupDialog::StartupDialog(MaterialEditor*parent) : QDialog(parent), mEditor( parent )
			{
				ui.setupUi(this);
				mMapper = new QSignalMapper(ui.recentProjects);
				ui.recentProjects->setColumnCount(2);
				ui.recentProjects->horizontalHeader()->setStretchLastSection(true);
				ui.recentProjects->setColumnHidden( 0, true );
				ui.recentProjects->horizontalHeader()->setVisible(false);
				ui.recentProjects->verticalHeader()->setVisible(false);
			}

			StartupDialog::~StartupDialog(void)
			{
			}
			bool StartupDialog::eventFilter( QObject * obj, QEvent * evt )
			{
				if( evt->type() == QEvent::FocusIn )
				{
					if( obj == qobject_cast<QObject*>(ui.newProj) )
					{
						on_newProj_focus();
					}
					else if( obj == qobject_cast<QObject*>(ui.openProj) )
					{
						on_openProj_focus();
					}
				}
				return QDialog::eventFilter( obj, evt );
			}
			int StartupDialog::exec()
			{
				ui.recentProjects->clearContents();
				RecentProjectList lst;
				for( auto i=lst.begin(); i!=lst.end(); i++ )
				{
					int cnt = ui.recentProjects->rowCount();
					ui.recentProjects->insertRow(cnt);
					QPushButton * lbl = new QPushButton(TO_QT(i->fileName()), ui.recentProjects);
					connect( lbl, SIGNAL(clicked(bool)), mMapper, SLOT(map()) );
					mMapper->setMapping( lbl, TO_QT(i->str()) );
					ui.recentProjects->setCellWidget( cnt, 0, new QLabel(TO_QT(i->dirName()), ui.recentProjects) );
					ui.recentProjects->setCellWidget( cnt, 1, lbl );
				}
				connect( mMapper, SIGNAL(mapped(const QString&)), this, SLOT(on_loadRecentProj(const QString&)) );
				return QDialog::exec();
			}
			void StartupDialog::on_loadRecentProj( const QString& str )
			{
				try
				{
					mEditor->getProject()->openProject( FROM_QT(str) );
					mEditor->updateUi();
					accept();
				}
				catch( std::exception & e )
				{
					QMessageBox::warning( this, "Cannot load recent project", e.what() );
				}
			}
			void StartupDialog::on_newProj_clicked()
			{
				try
				{
					mEditor->getProject()->newProject();
					accept();
				}
				catch( std::exception & e )
				{
					QMessageBox::warning( this, "Cannot load recent project", e.what() );
				}
					mEditor->updateUi();
			}
			void StartupDialog::on_openProj_clicked()
			{
				try
				{
					mEditor->getProject()->openProject();
					accept();
				}
				catch( std::exception & e )
				{
					QMessageBox::warning( this, "Cannot load recent project", e.what() );
				}
				mEditor->updateUi();
			}
			void StartupDialog::on_newProj_focus()
			{
				ui.desc->setText("Create a new project from a template list,\
Supported file types:\
\tMaterial\
\tCompositor\
\tParticle\
\tCG\
\tHLSL\
\tGLSL\n");
			}
			void StartupDialog::on_openProj_focus()
			{
				ui.desc->setText("Opens an existing project,\
Supported file types:\
\tMaterial\
\tCompositor\
\tParticle\
\tCG\
\tHLSL\
\tGLSL\n");
			}
		};
	};
};